//获取应用实例
const app = getApp();

Page({
    data:{
        season_list: [],
        order_list: [],
        now_page: 0,
        limit: 30,
        keywords: '',
        list_total: 0,
        season_id: -1,
        supplier_id: 0,
        year: 0,
        is_show_shade: false,
        menu_x: 0,
        menu_y: 0,
        is_show_menu: false,
        is_show_shade_screen: false
    },
    onLoad: function () {
        // 设置标题
        wx.setNavigationBarTitle({
            title: '样板汇总'
        });
        // 获取宽高
        var __this = this;
        setTimeout(function () {
            __this.setData({
                menu_x: wx.getSystemInfoSync().windowWidth - 60,
                menu_y: wx.getSystemInfoSync().windowHeight - 70,
            });
        });
        // 获取季节列表
        this.get_season_list();
    },

    // 显示时
    onShow: function () {
        var __this = this;
        // 验证是否登录
        if (!app.check_is_login(false, true)) {
            // 清空数据
            __this.setData({
                season_list: [],
                order_list: [],
                now_page: 0,
                limit: 50,
                list_total: 0,
                keywords: '',
                season_id: -1,
                supplier_id: 0,
                supplier_list: [],
            });
            // 重新获取数据
            wx.showModal({
                title: '提示',
                content: '请登录',
                complete(res) {
                    // 获取季节列表
                    __this.get_season_list();
                }
            });
        }
    },

    // 页面触底时执行
    onReachBottom: function () {
        // if (this.data.season_id > -1) {
        //     this.get_order_list();
        // }
    },

    // 监听下拉[刷新]
    onPullDownRefresh: function () {
        this.setData({
            season_list: [],
            order_list: [],
            now_page: 0,
            limit: 100,
            list_total: 0,
            keywords: '',
            season_id: -1,
            supplier_id: 0,
        });
        // 获取季节
        this.get_season_list();
        // 停止下拉
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    // 获取季节列表
    get_season_list: function () {
        // 发起请求获取邀请码
        var result = app.request_api('admin/template/getTemplateSummary');
        result.then(res => {
            console.log(res)
            if (res.length > 0) {
                res.forEach(function (item, index) {
                    res[index].is_show = 0;
                });
            }
            // var is_show_shade = true;
            // if (res.is_have_auth == 1) {
            //     is_show_shade = false;
            // }
            this.setData({
                season_list: res
            })
        });
    },

    // 显示或隐藏订单列表
    toggle_show_list: function (events) {
        var __this = this;
        var index = events.currentTarget.dataset.index;
        var is_show = __this.data.season_list[index].is_show == 1 ? 0 : 1;
        var season_id = -1;
        // 显示或隐藏时
        if (is_show == 1) {
            season_id = __this.data.season_list[index].value;
        }
        // 关闭全部
        __this.data.season_list.forEach(function (item, index2) {
            __this.data.season_list[index2].is_show = 0;
        });
        // 展示自己
        __this.data.season_list[index].is_show = is_show;
        // 重置
        __this.setData({
            season_list: __this.data.season_list,
            season_id: season_id,
            order_list: [],
            now_page: 0,
            list_total: 0,
            keywords: '',
            supplier_id: 0,
        });
        // 请求获取订单信息
        if (is_show == 1) {
            this.get_order_list();
        }
    },

    // 获取订单信息
    get_order_list: function (is_search = 0) {
        var __this = this;
        // 验证是否超出最大页码
        if ((this.data.order_list.length >= this.data.list_total) && this.data.now_page > 0) {
            return false;
        }
        // 请求参数
        var req_data = {
            page: this.data.now_page + 1,
            quick_search: __this.data.keywords,
            limit: __this.data.limit,
            season: __this.data.season_id,
            year: __this.data.year,
        };
        console.log(req_data)
        // 发起请求
        var result = app.request_api('admin/Template/index?order=status,asc', req_data);
        result.then(res => {
            console.log(res);
            if (is_search == 1) {
                this.setData({
                    order_list: [],
                    list_total: 0,
                    now_page: 0,
                })
            }
            this.setData({
                order_list: this.data.order_list.concat(res.list),
                list_total: res.total,
                now_page: this.data.now_page + 1
            });
        });
    },

    // 搜索
    GoSearch: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            keywords: keywords,
            order_list: [],
            list_total: 0,
            now_page: 0,
        });
        this.get_order_list(1);
    },

    // 预览图片
    PreviewImg: function (events) {
        var pic_arr = events.currentTarget.dataset.pic;
        if (pic_arr.length == 0) {
            return false;
        }
        // 调用
        wx.previewImage({
            current: pic_arr[0],
            urls: pic_arr
        })
    },

    // 跳转到详情
    order_detail: function (events) {
        var __this = this;
        var index = events.currentTarget.dataset.index;
        wx.navigateTo({
            url: '/pages/template_detail/template_detail?template_id=' + __this.data.order_list[index].template_id,
            // 当点击生产按钮时
            changeProductData: function (data) {

            },
            // 当点击关闭生产按钮时
            changeCloseData: function (data) {

            }
        });
    },

    // 跳转到物料页
    wuliao_detail: function (events) {
        var __this = this;
        var index = events.currentTarget.dataset.index;
        wx.navigateTo({
            url: '/pages/total_bom/total_bom_list?template_no=' + __this.data.order_list[index].template_no,
        });
    },

    // 点击菜单时
    menu_list_toggle: function () {
        var is_show_menu = true;
        var is_show_shade_screen= true;
        if (this.data.is_show_menu) {
            is_show_menu = false;
            is_show_shade_screen = false;
        }
        this.setData({
            is_show_menu: is_show_menu,
            is_show_shade_screen: is_show_shade_screen
        });
    },

})
